package com.ruoyi.datastatistics.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;


import com.ruoyi.datastatistics.domain.PublicShare;
import com.ruoyi.datastatistics.domain.PublicShareUserJoin;
import com.ruoyi.datastatistics.mapper.PublicShareMapper;
import com.ruoyi.datastatistics.mapper.PublicShareUserJoinMapper;
import com.ruoyi.datastatistics.service.IPublicShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
/**
 * 公摊配置Service业务层处理
 * 
 * @author gdqt
 * @date 2025-05-13
 */
@Service
public class PublicShareServiceImpl extends ServiceImpl<PublicShareMapper, PublicShare> implements IPublicShareService
{
    @Autowired
    private PublicShareMapper publicShareMapper;

    @Autowired
    private PublicShareUserJoinMapper publicShareUserJoinMapper;

    /**
     * 查询公摊配置
     * 
     * @param id 公摊配置主键
     * @return 公摊配置
     */
    @Override
    public PublicShare selectPublicShareById(Long id)
    {

        return publicShareMapper.selectById(id);
    }

    /**
     * 查询公摊配置列表
     * 
     * @param publicShare 公摊配置
     * @return 公摊配置
     */
    @Override
    public List<PublicShare> selectPublicShareList(PublicShare publicShare)
    {
        LambdaQueryWrapper<PublicShare> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(publicShare.getTenantId())){
            queryWrapper.eq(PublicShare::getTenantId,publicShare.getTenantId());
        }
        if(!StringUtils.isEmpty(publicShare.getAreaId())){
            queryWrapper.eq(PublicShare::getAreaId,publicShare.getAreaId());
        }
        if(!StringUtils.isEmpty(publicShare.getPublicShareName())){
            queryWrapper.eq(PublicShare::getPublicShareName,publicShare.getPublicShareName());
        }
        if(publicShare.getEnergyType() != null){
            queryWrapper.eq(PublicShare::getEnergyType,publicShare.getEnergyType());
        }
        if(!StringUtils.isEmpty(publicShare.getAddr())){
            queryWrapper.eq(PublicShare::getAddr,publicShare.getAddr());
        }
        return publicShareMapper.selectList(queryWrapper);
    }

    /**
     * 新增公摊配置
     * 
     * @param publicShare 公摊配置
     * @return 结果
     */
    @Override
    public int insertPublicShare(PublicShare publicShare)
    {
        return publicShareMapper.insert(publicShare);
    }

    /**
     * 修改公摊配置
     * 
     * @param publicShare 公摊配置
     * @return 结果
     */
    @Override
    public int updatePublicShare(PublicShare publicShare)
    {
        return publicShareMapper.updateById(publicShare);
    }

    /**
     * 批量删除公摊配置
     * 
     * @param ids 需要删除的公摊配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePublicShareByIds(Long[] ids)
    {
        //先删除关联表中记录
        for(Long id : ids){
            publicShareUserJoinMapper.delete(new LambdaQueryWrapper<PublicShareUserJoin>().eq(PublicShareUserJoin::getPublicShareId,id));
        }
        //再删除公摊配置表中记录
        return publicShareMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除公摊配置信息
     * 
     * @param id 公摊配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePublicShareById(Long id)
    {
        return publicShareMapper.deleteById(id);
    }

    @Override
    public List<PublicShare> selectPublicShareListInSql(PublicShare publicShare) {
        return publicShareMapper.selectPublicShareListInSql(publicShare);
    }

    /**
    * @Description: 一键过户
    * @date: 2025/7/9 10:45
    * @author: zt
    */
    @Override
    public int transferPublicShare(PublicShare publicShare) {
        String sourceUserId = publicShare.getSourceUserId();
        String targetUserId = publicShare.getTargetUserId();
        Long[] publicShareIds = publicShare.getPublicShareIds();
        //1.更新原用户在各个公摊下的状态改为离场
        PublicShareUserJoin lcEntity = new PublicShareUserJoin();
        lcEntity.setStatus(2);
        publicShareUserJoinMapper.update(lcEntity,new LambdaQueryWrapper<PublicShareUserJoin>().eq(PublicShareUserJoin::getUserId,sourceUserId));
        //先查询公摊下所有sourceUserId的挂接信息
        List<PublicShareUserJoin> sourceJoinList = publicShareUserJoinMapper.selectList(new LambdaQueryWrapper<PublicShareUserJoin>()
                .eq(PublicShareUserJoin::getUserId,sourceUserId)
                .in(PublicShareUserJoin::getPublicShareId,Arrays.asList(publicShareIds)));
        for (PublicShareUserJoin tempJoin : sourceJoinList) {
            //2.新增新用户
            tempJoin.setId(null);
            tempJoin.setUserId(targetUserId);
            tempJoin.setUpdateTime(new Date());
            tempJoin.setStatus(1);
            publicShareUserJoinMapper.insert(tempJoin);
        }
        return 1;
    }
}
